<?php
namespace Common\Models;
use Common\BaseModel;
class BShopOrderCar extends BaseModel
{
    public $res;
    public $expire;
    public function initialize()
    {
        parent::initialize();
        $this->setSource('b_shop_order_car');
    }


    /**
     * 获取购物车列表
     */
    public function getCarList($params){
        $conditions = "c.user_id = '{$params['user_id']}'";
        $sql=<<<EFO
select c.*,g.deleted,g.status,g.admin_status from b_shop_order_car as c
left join b_shop_goods as g on g.goods_sn = c.goods_id
where $conditions
EFO;
        $get_goods_car_list = $this->getDI()->get('dbMaster')->fetchAll($sql);
        $responseResult = [];

        if($get_goods_car_list){
            $shopIdsArr = array_unique(array_column($get_goods_car_list,'shop_id'));
            $shop_ids = implode(",",$shopIdsArr);
            $BShopUserDb = new BshopUsersInfo();
            $shops = $BShopUserDb->find(array(
                "conditions"=>"shop_id in({$shop_ids})",
                "columns"=>"shop_id,shop_name,shop_introduction,is_disable"
            ))->toarray();
            foreach ($shops as $key=>$value){
                $responseResult[$key] = $value;
                foreach ($get_goods_car_list as $gkey=>$gvalue){
                    if($value['shop_id'] == $gvalue['shop_id']){
                        if($gvalue['status'] == 1 && $gvalue['admin_status'] == 1 && $value['is_disable'] == 1 && $gvalue['deleted'] == 0){ //判断商品是否有效
                            $gvalue['valid'] = true;
                        }else{
                            $gvalue['valid'] = false;
                        }
                        unset($gvalue['admin_status']);
                        unset($gvalue['status']);
                        unset($gvalue['deleted']);
                        $responseResult[$key]['goods_list'][] = $gvalue;
                    }
                }
            }
        }
        return $responseResult;
    }


}
